//
//  UserInfoTable.swift
//  ANOA
//
//  Created by 邱仙凯 on 2024/8/29.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit
 

class UserInfoTable:UITableView,UITableViewDelegate,UITableViewDataSource {
    var dataArray = [AuditUserModel]()
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        self.delegate = self
        self.dataSource = self
        self.rowHeight = 120
        self.register(UserInfoCell.self, forCellReuseIdentifier: UserInfoCell.identifier)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: UserInfoCell.identifier, for: indexPath) as? UserInfoCell else {
            fatalError()
        }
        let one = dataArray[indexPath.row]
        cell.lab1.text = "姓名：\(one.cName.unwrap)"
        cell.lab2.text = "单位：\(one.orgName.unwrap)"
        cell.lab3.text = "部门：\(one.cUnitname.unwrap)"
        cell.telStr = one.cMobiletel.unwrap
     
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let one = dataArray[indexPath.row]

        let phone = "tel://" + one.cMobiletel.unwrap
        if UIApplication.shared.canOpenURL(URL(string: phone)!) {

            UIApplication.shared.open(URL(string: phone)!, options: [:], completionHandler: { result in
            })

        } else {
            KSToast("电话错误")
        }
    }
    
}

class UserInfoCell: UITableViewCell {
    
    var btnClosure:((String)->())?
    var lab1:UILabel!
    var lab2:UILabel!
    var lab3:UILabel!
    private var btn:AuditPInfoButton!
    var telStr:String = "" {
        didSet {
            btn.setTitle("电话：\(telStr)", for: .normal)
        }
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
        self.backgroundColor = .white
        lab1 = UILabel()
        lab1.font = UIFont.systemFont(ofSize: 16)
        lab2 = UILabel()
        lab2.font = UIFont.systemFont(ofSize: 16)
        lab2.numberOfLines = 2
        lab3 = UILabel()
        lab3.font = UIFont.systemFont(ofSize: 16)
        btn = AuditPInfoButton()
        btn.setTitle("电话：17706019256", for: .normal)
        btn.setTitleColor(Theme.primaryColor(), for: .normal)
        btn.setImage(UIImage(named: "dianhua"), for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        
        self.addSubview(lab1)
        self.addSubview(lab2)
        self.addSubview(lab3)
        self.addSubview(btn)
        
        lab1.snp.makeConstraints { (make) in
            make.top.equalTo(self).offset(5)
            make.left.equalTo(self).offset(10)
        }
        lab2.snp.makeConstraints { (make) in
            make.top.equalTo(lab1.snp.bottom).offset(5)
            make.left.equalTo(self).offset(10)
            make.right.equalTo(self).offset(-10)
        }
        lab3.snp.makeConstraints { (make) in
            make.top.equalTo(lab2.snp.bottom).offset(5)
            make.left.equalTo(self).offset(10)
        }
        btn.snp.makeConstraints { (make) in
            make.top.equalTo(lab3.snp.bottom).offset(5)
            make.left.equalTo(self).offset(10)
            make.width.equalTo(180)
            make.height.equalTo(25)
        }
    }
    
  
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
